/*
 * vmscr.c
 */

/*
 * This program uses DOS video function to get character from screen.
 * It causes application error in Win32 environment.
 */
#ifndef _MSDOS
#error	DOS version only.
#endif	/* _MSDOS	*/

#include <stdio.h>
#include "vmbdoor.h"

/*
 *	Get cursor position BIOS call
 */
 
int GetCursorPos()
{
	__asm {
		mov ah, 03h
		xor bh, bh
		int 10h
		
		mov ax, dx		; high = row, low = col
	}
}

/*
 *	Get character from screen
 */

char GetCharacterAt(unsigned char row, unsigned char col)
{
	__asm {
		; set cursor position
		mov ah, 02h
		xor bh, bh
		mov dh, row
		mov dl, col
		int 10h
		
		; read character at the cursor position
		mov ah, 08h
		xor bh, bh
		int 10h
	}
}

/*
 * main
 */

int main ()
{
	char buf[2048];
	uint32 length, command;
	unsigned char row, col;
	int i;
	
	/* are we running in VMware ? 	*/
	if (VMGetVersion() == 0) {
		printf("Not running inside VMware.\n");
		return 0;
	}

	/* get current curosor position */
	i = GetCursorPos();
	
	/* get characters on screen */
	length = 0;
	
	for (row = 0; row < 25; row++) {
		for (col = 0; col < 80; col++) {
			buf[length] = GetCharacterAt(row, col);
			
			if (buf[length] < ' ') {
				buf[length] = ' ';
			}
			length++;
		}
		
		/* remove trailing blank on each line */
		while (length && buf[length - 1] == ' ') {
			length--;
		}
		buf[length++] = '\n';
	}
	buf[length++] = '\0';

	/* restore original cursor position */
	GetCharacterAt(i >> 8, i & 0xff);

	/* send clipboard data length	*/
	command = VMCMD_SET_CLIPLEN;
	VMBackDoor(NULL, &length, &command, NULL);

	i = 0;
	
	while (i < length) {
		command = VMCMD_SET_CLIPDATA;
		VMBackDoor(NULL, (uint32 *)(buf + i), &command, NULL);

		i += 4;
	}
	return 1;
}
/* End of File */
